<div class="table-body">
    <table id="userTable" lay-filter="userTable"></table>
</div>
<script type="text/html" id="smsTpl">
    <input type="checkbox" name="sms" value="{{d.code}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="smsDemo" {{ d.sms == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="messageTpl">
    <input type="checkbox" name="message" value="{{d.code}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="smsDemo" {{ d.message == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="wxTpl">
    <input type="checkbox" name="wx_tpl_message" value="{{d.code}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="smsDemo" {{ d.wx_tpl_message == 1 ? 'checked' : '' }}>
</script>

<script>
    layui.use(['form', 'layedit', 'laydate','table'], function(){
        layui.table.render({
            elem: '#userTable',
            height: 'full',
            cellMinWidth: '80',
            page: false,
            limit:'20',
            url: "{:url('MessageCenter/index')}?_ajax=1",
            id:'userTable',
            cols: [[
                {type:'numbers'},
                {field: 'code', width:300, title: '消息节点',templet:function(data){
                    var html = '';
                    html += data.name + '('+data.code+')';
                    return html;
                }},
                {field:'sms', title:'短信', width:100, templet: '#smsTpl', unresize: true},
                {field:'message', title:'站内消息', width:100, templet: '#messageTpl', unresize: true},
                {field:'wx_tpl_message', title:'微信消息', width:100, templet: '#wxTpl', unresize: true}
            ]]
        });
        //监听操作
        layui.form.on('switch(smsDemo)', function(obj){
            var data = {
                code:this.value
            }


            if(obj.elem.checked){
                data[this.name] = 1;
            }else{
                data[this.name] = 2;
            }

            $.ajax({
                type:'post',
                url: "{:url('MessageCenter/edit')}",
                data:data,
                success:function(e){
                    try {
                        var allData=JSON.parse(e);
                        if(typeof allData == 'object' && allData ){
                            layer.msg(allData.msg);
                            //失败
                            obj.elem.checked = !obj.elem.checked;
                            layui.form.render('checkbox');

                            return false;
                        }
                    } catch(e) {
                    }

                    if(e.status == "false") {
                        //失败
                        obj.elem.checked = !obj.elem.checked;
                        layui.form.render('checkbox');
                    }
                    
                    layer.msg(e.msg);
                }
            });
        });

    });
</script>
